import { roomOwner } from "../tools/baseTools"

const progressManager = {
    // 进程管理白名单
    // checkSelfList: ['source', STRUCTURE_CONTROLLER,STRUCTURE_SPAWN, STRUCTURE_EXTENSION,STRUCTURE_CONTAINER],
    checkSelfList: ['source', STRUCTURE_CONTROLLER, STRUCTURE_SPAWN, STRUCTURE_EXTENSION, STRUCTURE_CONTAINER],
    init: function (params = {}) {
        const { room, roomName, owner } = params
        if (owner != 0) return
        room.init()
        this.checkSelfList.map(structureType => {
            if (!room.memory['structure'][structureType]) room.memory['structure'][structureType] = {}
            Object.keys(room.memory['structure'][structureType]).map(id => {
                let obj = Game.getObjectById(id)
                if (!obj || (obj.progress != obj.progressTotal && obj.structureType != STRUCTURE_CONTROLLER)) return // 存在进度条，说明是工地
                obj.init()
            })
        })
    },
    /**房间内的建筑执行任务 */
    run: function () {
        Object.keys(Memory.rooms).map(roomName => {
            let room = Game.rooms[roomName]
            switch (roomOwner(room)) {
                case 0:
                    this.checkSelfList.map(structureType => {
                        Object.keys(Memory.rooms[roomName]['structure'][structureType]).map(id => {
                            let obj = Game.getObjectById(id)
                            if (!obj || (obj.progress != obj.progressTotal && obj.structureType != STRUCTURE_CONTROLLER)) return // 存在进度条，说明是工地
                            obj.run(obj)
                        })
                    })
                    break;

                case 1:
                    // console.log(`${roomName}房间无归属`);
                    break
                case 2:
                    // console.log(`${roomName}房间无视野`);
                    break
                case 3:
                    // console.log(`${roomName}房间已经被${room.controller.owner}占领`);
                    break

                default:
                    break;
            }
        })
    },
    /**房间及房间内更新信息 */
    checkSelf: function () {
        Object.keys(Memory.rooms).map(roomName => {
            let room = Game.rooms[roomName]
            switch (roomOwner(room)) {  //对不同归属的房间有不同的处理方法
                case 0:
                    room.checkSelf()
                    this.checkSelfList.map(structureType => {
                        Object.keys(Memory.rooms[roomName]['structure'][structureType]).map(id => {
                            let obj = Game.getObjectById(id)
                            if (!obj || (obj.progress != obj.progressTotal && obj.structureType != STRUCTURE_CONTROLLER)) return // 存在进度条，说明是工地
                            obj.checkSelf()
                        })
                    })
                    break;

                case 1:
                    // console.log(`${roomName}房间无归属`);
                    break
                case 2:
                    // console.log(`${roomName}房间无视野`);
                    break
                case 3:
                    // console.log(`${roomName}房间已经被${room.controller.owner}占领`);
                    break
                default:
                    break;
            }
        })
    },
    /**自检并推送任务 */
    pushTask: function () {
        Object.keys(Memory.rooms).map(roomName => {
            let room = Game.rooms[roomName]
            room.pushTask()
            // this.checkSelfList.map(structureType => {
            //     Object.keys(Memory.rooms[roomName]['structure'][structureType]).map(id => {
            //         let obj = Game.getObjectById(id)
            //         obj.pushTask()
            //     })
            // })
        })
    },
}


export default progressManager